package com.zenithmind.classroom.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.classroom.pojo.dto.CourseCreateDTO;
import com.zenithmind.classroom.pojo.dto.CourseQueryDTO;
import com.zenithmind.classroom.pojo.dto.CourseUpdateDTO;
import com.zenithmind.classroom.pojo.vo.CourseDetailVO;
import com.zenithmind.classroom.pojo.vo.CourseStatisticsVO;
import com.zenithmind.classroom.pojo.vo.CourseVO;
import com.zenithmind.classroom.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "课程管理", description = "课程管理相关接口")
@RestController
@RequestMapping("/api/v1/courses")
@RequiredArgsConstructor
public class CourseController {

    private final CourseService courseService;

    @Operation(summary = "创建课程", description = "创建新的课程")
    @PostMapping
    public Result<CourseVO> createCourse(@Valid @RequestBody CourseCreateDTO createDTO) {
        CourseVO course = courseService.createCourse(createDTO);
        return Result.success(course);
    }

    @Operation(summary = "更新课程", description = "更新课程信息")
    @PutMapping("/{id}")
    public Result<Boolean> updateCourse(
            @Parameter(description = "课程ID") @PathVariable String id,
            @Valid @RequestBody CourseUpdateDTO updateDTO) {
        Boolean result = courseService.updateCourse(id, updateDTO);
        return Result.success(result);
    }

    @Operation(summary = "删除课程", description = "删除指定课程")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteCourse(@Parameter(description = "课程ID") @PathVariable String id) {
        Boolean result = courseService.deleteCourse(id);
        return Result.success(result);
    }

    @Operation(summary = "发布课程", description = "发布指定课程")
    @PostMapping("/{id}/publish")
    public Result<Boolean> publishCourse(@Parameter(description = "课程ID") @PathVariable String id) {
        Boolean result = courseService.publishCourse(id);
        return Result.success(result);
    }

    @Operation(summary = "下架课程", description = "下架指定课程")
    @PostMapping("/{id}/unpublish")
    public Result<Boolean> unpublishCourse(@Parameter(description = "课程ID") @PathVariable String id) {
        Boolean result = courseService.unpublishCourse(id);
        return Result.success(result);
    }

    @Operation(summary = "分页查询课程", description = "分页查询课程列表")
    @GetMapping
    public Result<PageResult<CourseVO>> getCoursePage(@Valid CourseQueryDTO queryDTO) {
        PageResult<CourseVO> result = courseService.getCoursePage(queryDTO);
        return Result.success(result);
    }

    @Operation(summary = "分页查询课程（兼容旧接口）", description = "分页查询课程列表")
    @GetMapping("/page")
    public Result<PageResult<CourseVO>> getCoursePageLegacy(@Valid CourseQueryDTO queryDTO) {
        PageResult<CourseVO> result = courseService.getCoursePage(queryDTO);
        return Result.success(result);
    }

    @Operation(summary = "获取课程详情", description = "获取课程详细信息")
    @GetMapping("/{id}")
    public Result<CourseDetailVO> getCourseDetail(@Parameter(description = "课程ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        CourseDetailVO course = courseService.getCourseDetail(id, userId);
        return Result.success(course);
    }

    @Operation(summary = "获取推荐课程", description = "获取个性化推荐课程")
    @GetMapping("/recommend")
    public Result<List<CourseVO>> getRecommendCourses(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        String userId = UserContext.getUserId();
        List<CourseVO> courses = courseService.getRecommendCourses(userId, limit);
        return Result.success(courses);
    }

    @Operation(summary = "获取热门课程", description = "获取热门课程列表")
    @GetMapping("/hot")
    public Result<List<CourseVO>> getHotCourses(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<CourseVO> courses = courseService.getHotCourses(limit);
        return Result.success(courses);
    }

    @Operation(summary = "获取最新课程", description = "获取最新发布的课程")
    @GetMapping("/latest")
    public Result<List<CourseVO>> getLatestCourses(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<CourseVO> courses = courseService.getLatestCourses(limit);
        return Result.success(courses);
    }

    @Operation(summary = "搜索课程", description = "根据关键词搜索课程")
    @GetMapping("/search")
    public Result<List<CourseVO>> searchCourses(
            @Parameter(description = "关键词") @RequestParam String keyword,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<CourseVO> courses = courseService.searchCourses(keyword, limit);
        return Result.success(courses);
    }

    @Operation(summary = "获取用户课程", description = "获取用户的课程列表")
    @GetMapping("/user")
    public Result<List<CourseVO>> getUserCourses(
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        String userId = UserContext.getUserId();
        List<CourseVO> courses = courseService.getUserCourses(userId, status, limit);
        return Result.success(courses);
    }

    @Operation(summary = "获取讲师课程", description = "获取指定讲师的课程")
    @GetMapping("/instructor/{instructorId}")
    public Result<List<CourseVO>> getInstructorCourses(
            @Parameter(description = "讲师ID") @PathVariable String instructorId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<CourseVO> courses = courseService.getInstructorCourses(instructorId, limit);
        return Result.success(courses);
    }

    @Operation(summary = "获取分类课程", description = "获取指定分类的课程")
    @GetMapping("/category/{categoryId}")
    public Result<List<CourseVO>> getCategoryCourses(
            @Parameter(description = "分类ID") @PathVariable String categoryId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<CourseVO> courses = courseService.getCategoryCourses(categoryId, limit);
        return Result.success(courses);
    }

    @Operation(summary = "获取相关课程", description = "获取相关推荐课程")
    @GetMapping("/{id}/related")
    public Result<List<CourseVO>> getRelatedCourses(
            @Parameter(description = "课程ID") @PathVariable String id,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "5") Integer limit) {
        List<CourseVO> courses = courseService.getRelatedCourses(id, limit);
        return Result.success(courses);
    }

    @Operation(summary = "收藏课程", description = "收藏指定课程")
    @PostMapping("/{id}/favorite")
    public Result<Boolean> favoriteCourse(@Parameter(description = "课程ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        Boolean result = courseService.favoriteCourse(id, userId);
        return Result.success(result);
    }

    @Operation(summary = "取消收藏课程", description = "取消收藏指定课程")
    @DeleteMapping("/{id}/favorite")
    public Result<Boolean> unfavoriteCourse(@Parameter(description = "课程ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        Boolean result = courseService.unfavoriteCourse(id, userId);
        return Result.success(result);
    }

    @Operation(summary = "获取收藏课程", description = "获取用户收藏的课程")
    @GetMapping("/favorites")
    public Result<List<CourseVO>> getFavoriteCourses(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        String userId = UserContext.getUserId();
        List<CourseVO> courses = courseService.getFavoriteCourses(userId, limit);
        return Result.success(courses);
    }

    @Operation(summary = "评价课程", description = "对课程进行评价")
    @PostMapping("/{id}/rate")
    public Result<Boolean> rateCourse(
            @Parameter(description = "课程ID") @PathVariable String id,
            @Parameter(description = "评分") @RequestParam Integer rating,
            @Parameter(description = "评价内容") @RequestParam(required = false) String comment) {
        String userId = UserContext.getUserId();
        Boolean result = courseService.rateCourse(id, userId, rating, comment);
        return Result.success(result);
    }

    @Operation(summary = "获取课程统计", description = "获取课程统计信息")
    @GetMapping("/{id}/statistics")
    public Result<CourseStatisticsVO> getCourseStatistics(@Parameter(description = "课程ID") @PathVariable String id) {
        CourseStatisticsVO statistics = courseService.getCourseStatistics(id);
        return Result.success(statistics);
    }

    @Operation(summary = "复制课程", description = "复制现有课程")
    @PostMapping("/{id}/copy")
    public Result<CourseVO> copyCourse(
            @Parameter(description = "课程ID") @PathVariable String id,
            @Parameter(description = "新课程标题") @RequestParam String title) {
        CourseVO course = courseService.copyCourse(id, title);
        return Result.success(course);
    }

    @Operation(summary = "审核课程", description = "审核课程内容")
    @PostMapping("/{id}/review")
    public Result<Boolean> reviewCourse(
            @Parameter(description = "课程ID") @PathVariable String id,
            @Parameter(description = "是否通过") @RequestParam Boolean approved,
            @Parameter(description = "审核意见") @RequestParam(required = false) String comment) {
        Boolean result = courseService.reviewCourse(id, approved, comment);
        return Result.success(result);
    }

    @Operation(summary = "批量导入课程", description = "批量导入课程数据")
    @PostMapping("/batch/import")
    public Result<Integer> batchImportCourses(@Valid @RequestBody List<CourseCreateDTO> courses) {
        Integer count = courseService.batchImportCourses(courses);
        return Result.success(count);
    }

    @Operation(summary = "导出课程", description = "导出指定课程数据")
    @PostMapping("/batch/export")
    public Result<List<CourseVO>> exportCourses(@RequestBody List<String> courseIds) {
        List<CourseVO> courses = courseService.exportCourses(courseIds);
        return Result.success(courses);
    }
}
